package com.example.ncov_management_system.controller;

import com.example.ncov_management_system.entity.Clbum;
import com.example.ncov_management_system.entity.Student;
import com.example.ncov_management_system.entity.Teacher;
import com.example.ncov_management_system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelExtensionsKt;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/teacher")
@Slf4j
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ClbumService clbumService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("teachers", teacherService.list());
        return "teacher_list";
    }

    @GetMapping("/to_add")
    public String toAdd(Model model) {
        model.addAttribute("clbums", clbumService.findByTeacherIsNull());
        return "teacher_add";
    }

    @PostMapping("/add")
    public String add(Teacher teacher, long[] clbum_ids) {
        log.info("ids:{}", clbum_ids);
        teacherService.add(teacher, clbum_ids);
        return "redirect:/teacher/list";
    }

    @GetMapping("/edit/{id}")

    public String info(@PathVariable long id, Model model) {
        model.addAttribute("teacher", teacherService.findById(id));
        model.addAttribute("clbums", clbumService.list());
        return "teacher_edit";
    }

    @PostMapping("/edit")
    public String edit(Teacher teacher, long[] clbum_ids) {
        log.info("clbums:{}",clbum_ids);
        teacherService.edit(teacher, clbum_ids);
        return "redirect:/teacher/list";
    }

    @GetMapping("/delete")
    public String delete(@RequestParam long id) {

        teacherService.delete(id);
        return "redirect:/teacher/list";
    }

    @GetMapping("/index")
    public String adminIndex(HttpSession session, Model model) {
        ArrayList<Map> list = new ArrayList<>();
        List<Student> reportedStudent = new ArrayList<>();
        List<Student> studentList = new ArrayList<>();
        Teacher teacher = teacherService.findById(((Teacher) session.getAttribute("teacher")).getId());
        for (Clbum clbum : teacher.getClbums()) {
            List<Student> students = clbum.getStudents();
            log.info("{}:{}",clbum.getName(),students);
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", clbum.getName());
            map.put("count", students.size());
            list.add(map);
            reportedStudent.addAll(recordService.findReportedStudentByClbumId(clbum.getId()));
            studentList.addAll(studentService.findByClbumId(clbum.getId()));
        }
        log.info("reported:{}",reportedStudent);
        model.addAttribute("clbums", list);
        List<Student> result = new ArrayList<>();
        for (Student student : studentList) {
            if (!reportedStudent.contains(student)) {
                result.add(student);
            }
        }
        log.info("result:{}",result);
        model.addAttribute("un_reportedCount", result.size());
        model.addAttribute("reportedCount", reportedStudent.size());
        return "teacher_index";
    }
}
